/**
 * @author Daniel
 */
//Global vars
var divs	= new Array();		//contains a divs id as the key, and an array of tiles for value

/*
 * addDiv
 * Add a divs ID to divs, with properties
 * id: id of the div that the tiles will be added to
 * params: optional array to override default properties
 */
function addDiv(id)
{
	args	= arguments;
	
	//Throw error if ID already exists
	if(divs[id] != undefined)
	{
		throw "addDiv(): Div ID is not empty";
	}
	else
	{
		if(typeof args[1] != "undefined")
		{
			divs[id] = {"params" : args[1]};
		}
		else
		{
			divs[id] = {"params" : "none"};
		}		
	}
}

/*
 * outTiles
 * output the tiles in location id to it's location
 * reads the first array element to override optional properties
 */
function outTiles(id)
{
	var i		= 0;
	var tile 	= divs[id];
	//Set defaults
	var width	= 2;	//2 tiles before new line
	
	//Override defaults
	if(typeof tile["params"]["width"] != "undefined")
	{
		width	= tile["params"]["width"];
	}
	
	for(var key in tile)
	{
		if(key != "params")
		{
			document.getElementById(id).innerHTML	+= tile[key];
			i++
			if(i == width)
			{
				document.getElementById(id).innerHTML	+= "<br>";
				i = 0;
			}
		}
	}
}

/*
 * addTile
 * Add a tile to a given location div
 * args structure:-
 * title: title, id: id, action: action, css: Array(css: val)
 */
function addTile(args){
	//Throw noID exception (purposely not caught) if an ID to append to is unspecified
	if(typeof args['id'] == "undefined")
	{
		throw "addTile: No ID was specified for the location of the tile";
	}
	var id		= args["id"];
	
	//Set defaults
	var title	= "Default";
	var action	= "return false;";
	var tile	= "<div class='tile' ";
	var css		= "style='";
	
	//Check if defaults should be overwritten
	if(typeof args["title"] != "undefined")
	{
		title	= args["title"];
	}
	if(typeof args["action"] != "undefined")
	{
		action	= args["action"];
	}
	
	//Check if any CSS properties exist
	if(typeof args["css"] != "undefined")
	{
		for(var key in args["css"])
		{
			css		+= key + ":" + args["css"][key] + "; ";
		}
		//Close css
		css			+= "' ";
		//Append css
		tile		+= css;
	}
	
	//Finish creating tile
	//onmouseover='this.style.cursor=\"hand\";' onmouseout='this.style.cursor=\"auto\";'
	tile	+= "onclick='" + action + "'><p class='tile-content'>" + title + "</p></div>";
	
	//Add tile to new location
	div		= divs[id];
	div[tile]	= tile;
}
